package com.hanhai.material.device.utils.kingdee;

import cn.hutool.core.map.MapBuilder;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hanhai.material.common.result.KingdeeResult;
import com.hanhai.material.device.model.TokenModel;
import com.hanhai.material.device.model.kingdeletemodel.Data;
import com.hanhai.material.device.model.kingdeletemodel.KingDeleteModel;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
@Log4j2
public class KingDeleteUtil {
    @Value("${kingdee.baseUrl}")
    String baseUrl;
    @Value("${kingdee.authorityCode}")
    String authorityCode;

    public String createToken() throws IOException {
        String url = baseUrl + "/Token/Create?authorityCode=" + authorityCode;
        HttpHeaders headers = new HttpHeaders();
        RestTemplate restTemplate = new RestTemplate();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        Map<Object, Object> map = new HashMap<>();
        HttpEntity<Map<Object, Object>> httpEntity = new HttpEntity<>(map, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        JSONObject jsonObject = JSON.parseObject(responseEntity.getBody());
        TokenModel tokenModel = jsonObject.getObject("Data", TokenModel.class);
        return tokenModel.getToken();
    }

    public KingdeeResult deleteProductReceiptByNo(String billNo) throws IOException {
        String token = this.createToken();
        String url = baseUrl + "/ProductReceipt/Delete?token=" + token;
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        Map<Object,Object> map= MapBuilder.create().put("Data",MapBuilder.create().put("FBillNo",billNo).build()).build();
        HttpEntity<Map<Object,Object>> httpEntity = new HttpEntity<>(map, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(responseEntity));
        log.error("Json:{}", JSONObject.toJSONString(jsonObject.getJSONObject("body")));
        return JSONObject.parseObject(JSONObject.toJSONString(jsonObject.getJSONObject("body")), KingdeeResult.class);
    }
    public KingdeeResult deleteTransferByNo(String billNo) throws IOException {
        String token = this.createToken();
        String url = baseUrl + "/Transfer/Delete?token=" + token;
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        Map<Object,Object> map= MapBuilder.create().put("Data",MapBuilder.create().put("FBillNo",billNo).build()).build();
        HttpEntity<Map<Object,Object>> httpEntity = new HttpEntity<>(map, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(responseEntity));
        log.error("Json:{}", JSONObject.toJSONString(jsonObject.getJSONObject("body")));
        return JSONObject.parseObject(JSONObject.toJSONString(jsonObject.getJSONObject("body")), KingdeeResult.class);
    }

    public KingDeleteModel getKingDeleteModel(String billNo){
        KingDeleteModel kingDeleteModel=new KingDeleteModel();
        Data data=new Data();
        data.setFBillNo(billNo);
        kingDeleteModel.setData(data);
        log.error("King:{}",JSONObject.toJSONString(kingDeleteModel));
        return kingDeleteModel;
    }

}
